﻿using CrawlerFramework.DataConverter;
using System.Data.Entity;

namespace RakutenProxy.DataConverter
{
    /// <summary>
    /// Migrate data from source entity to target entity
    /// </summary>
    /// <typeparam name="S">Source class</typeparam>
    /// <typeparam name="T">Target class</typeparam>
    public abstract class RakNopDataConverter<S, T> : BaseDataConverter
        where S : class
        where T : class
    {

        private DbContext _sourceContext;

        private DbContext _targetContext;

        /// <summary>
        /// Get RakutenDbContext instance
        /// </summary>
        public override System.Data.Entity.DbContext SourceContext
        {
            get
            {
                if (_sourceContext == null)
                    _sourceContext = new RakutenProxy.Model.Rakuten.RakutenDbContext();
                return _sourceContext;
            }
        }

        /// <summary>
        /// Get the NopCommerceEntities instance
        /// </summary>
        public override System.Data.Entity.DbContext TargetContext
        {
            get
            {
                if (_targetContext == null)
                    _targetContext = new RakutenProxy.Model.NopCommerce.NopCommerceEntities();

                return _targetContext;
            }
        }

        /// <summary>
        /// Perform creational action to get the target entity from input entity
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        protected abstract T GetObjectFromSource(S source);

        /// <summary>
        /// Perform update action to make the target entity up to date
        /// </summary>
        /// <param name="target"></param>
        /// <param name="source"></param>
        protected abstract void UpdateObjectFromSource(T target, S source);

        public RakNopDataConverter(DbContext sourceDbContext, DbContext targetDbContext) : base(sourceDbContext, targetDbContext)
        {
            this.TranslateService.SourceLanguage = "jp";
            this.TranslateService.TargetLanguage = "vi";
        }
    
    }
}